/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.p10.miage.gestionabsence.action;

import fr.p10.miage.gestionabsence.model.*;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author seidso
 */
public class AbsenceNowIntervenant1Action extends org.apache.struts.action.Action {

    /*
     * forward name="success" path=""
     */
    private static final String success = "listeEleveAbsenceDuJour";
    private static final String fail = "listeCoursAbsenceDuJour";
    private static final String exit = "Index";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {      
       
       if(SessionUtilisateur.isAuthenticated(request)){
          Intervenant intervenant  = SessionUtilisateur.getIntervenant(request); 
          int idAgenda;
          if(request.getParameter("id")!=null) 
              idAgenda = Integer.parseInt(request.getParameter("id"));
          else 
              idAgenda=  (Integer)request.getAttribute("id");
          
          if(idAgenda!=0){
                Agenda agenda =  AgendaBDD.trouverParId(idAgenda);
                if(agenda!=null && agenda.getIdI()==intervenant.getIdI()){
                    AgendaAffichage infos=AgendaBDD.getAgendaIntervenantById(agenda.getIdCr());
                    ArrayList<Etudiant> etudiants=EtudiantBDD.getList(agenda.getIdP());
                    if(etudiants!=null && !etudiants.isEmpty()){
                        ArrayList resultats=new ArrayList();
                        Boolean presence;
                        resultats.add(infos);
                        for(int i=0; i<etudiants.size(); i++){
                            ArrayList array=new ArrayList();
                            array.add(etudiants.get(i));
                            presence=PresenceBDD.trouverParEtudiantetAgenda(etudiants.get(i).getId(),agenda.getIdCr());
                            array.add(presence);
                            resultats.add(array);
                        }
                        request.setAttribute("listeEleve", resultats);
                        return mapping.findForward(success);
                    }
                }
                return mapping.findForward(fail);
            }
            return mapping.findForward(fail);
        }
        return mapping.findForward(exit);
    }
}
